package com.neusoft.yiyang.diet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.yiyang.common.entity.diet.FoodDict;
import com.neusoft.yiyang.common.entity.diet.MealRecord;
import com.neusoft.yiyang.common.entity.diet.MealRecordDetail;
import com.neusoft.yiyang.common.entity.diet.vto.FoodIdQuantity;
import com.neusoft.yiyang.common.entity.diet.vto.FoodItem;
import com.neusoft.yiyang.common.entity.diet.vto.FoodItemDetail;
import com.neusoft.yiyang.diet.mapper.FoodDictMapper;
import com.neusoft.yiyang.diet.mapper.MealRecordDetailMapper;
import com.neusoft.yiyang.diet.mapper.MealRecordMapper;
import com.neusoft.yiyang.diet.service.MealRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MealRecordServiceImpl implements MealRecordService {

    @Autowired
    private MealRecordMapper mealRecordMapper;

    @Autowired
    private MealRecordDetailMapper mealRecordDetailMapper;

    @Autowired
    private FoodDictMapper foodDictMapper;

    @Override
    public void addMealRecord(Map<String, Map<String, List<FoodIdQuantity>>> customerFoods,
                              Date date, Integer customerId, Integer userId) {
        QueryWrapper<MealRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId)
                .eq("meal_time", date);
        mealRecordMapper.delete(queryWrapper);
        // 遍历外层 Map
        for (Map.Entry<String, Map<String, List<FoodIdQuantity>>> outerEntry : customerFoods.entrySet()) {
            String mealType = outerEntry.getKey();
            Map<String, List<FoodIdQuantity>> mealFoods = outerEntry.getValue();
            MealRecord mealRecord1 = new MealRecord();
            mealRecord1.setMealType(mealType);
            mealRecord1.setCustomerId(customerId);
            mealRecord1.setMealTime(date);
            mealRecord1.setCaretakerId(userId);
            mealRecordMapper.insert(mealRecord1);

            Integer mealRecordId = mealRecord1.getMealRecordId();
            // 遍历内层 Map
            for (Map.Entry<String, List<FoodIdQuantity>> innerEntry : mealFoods.entrySet()) {
                String foodType = innerEntry.getKey();
                List<FoodIdQuantity> foodList = innerEntry.getValue();

                // 遍历 FoodIdQuantity 列表
                for (FoodIdQuantity food : foodList) {
                    Integer foodId = food.getFoodId();
                    Integer quantity = food.getQuantity();
                    MealRecordDetail mealRecordDetail = new MealRecordDetail();
                    mealRecordDetail.setMealRecordId(mealRecordId);
                    mealRecordDetail.setFoodId(foodId);
                    mealRecordDetail.setQuantity(quantity);
                    mealRecordDetailMapper.insert(mealRecordDetail);
                }
            }
        }
    }

    @Override
    public List<MealRecord> getAllMealRecords() {
        return mealRecordMapper.selectList(null);
    }

    @Override
    public List<MealRecord> getMealRecordsByCustomerId(Integer customerId) {
        return mealRecordMapper.selectByCustomerId(customerId);
    }

    @Override
    public void updateMealRecord(MealRecord mealRecord) {
        mealRecordMapper.updateById(mealRecord);
    }

    @Override
    public void deleteMealRecord(Integer mealRecordId) {
        mealRecordMapper.deleteById(mealRecordId);
    }

    @Override
    public Map<String, Map<String, List<FoodIdQuantity>>> getFoodRecord(Map<String, Map<String, FoodItemDetail>> mealData, Date date, Integer customerId) {
        Map<String, Map<String, List<FoodIdQuantity>>> result = new HashMap<>();
//         使用 Java 9+ 的 List.of() 方法初始化列表
        List<String> mealTypes = List.of("breakfast", "lunch", "dinner");
        List<String> categories = List.of("staple", "mainDish", "sideDish", "drink", "fruit");

        for (String mealType1 : mealTypes) {
            // 为每个餐食类型创建一个新的内层 Map
            Map<String, List<FoodIdQuantity>> foodItems = new HashMap<>();
            for (String category1 : categories) {
                List<FoodIdQuantity> foodIdQuantities = new ArrayList<>();
                foodItems.put(category1, foodIdQuantities);
            }
            result.put(mealType1, foodItems);
        }

        QueryWrapper<MealRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId)
                .eq("meal_time", date);
        List<MealRecord> mealRecords = mealRecordMapper.selectList(queryWrapper);
        for (MealRecord mealRecord : mealRecords) {
            String mealType = mealRecord.getMealType();
            QueryWrapper<MealRecordDetail> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("meal_record_id", mealRecord.getMealRecordId());
            List<MealRecordDetail> mealRecordDetails = mealRecordDetailMapper.selectList(queryWrapper1);
            Map<Integer, Integer> foodIdQuantityMap = new HashMap<>();
            List<Integer> foodIds = new ArrayList<>();
            for (MealRecordDetail mealRecordDetail : mealRecordDetails) {
                foodIdQuantityMap.put(mealRecordDetail.getFoodId(), mealRecordDetail.getQuantity());
                foodIds.add(mealRecordDetail.getFoodId());
            }
            List<FoodDict> foodDicts = new ArrayList<>();
            if (foodIds.size() > 0) {
                QueryWrapper<FoodDict> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.in("food_id", foodIds);
                foodDicts = foodDictMapper.selectList(queryWrapper2);
            }

            for (FoodDict foodDict : foodDicts) {
                String category = foodDict.getCategory();
                Integer foodId = foodDict.getFoodId();
                Integer quantity = foodIdQuantityMap.get(foodId);
                FoodIdQuantity foodIdQuantity = new FoodIdQuantity();
                foodIdQuantity.setFoodId(foodId);
                foodIdQuantity.setQuantity(quantity);
                result.get(mealType).get(category).add(foodIdQuantity);
            }

        }

        for (String mealType : mealTypes) {
            for (String category1 : categories) {
                List<FoodIdQuantity> foodIdQuantities = result.get(mealType).get(category1);
                int size = foodIdQuantities.size();
                for (int i = 0; i < mealData.get(mealType).get(category1).getLimit() - size; i++) {
                    FoodIdQuantity foodIdQuantity = new FoodIdQuantity();
                    foodIdQuantities.add(foodIdQuantity);
                }
            }
        }
        return result;
    }

    @Override
    public Map<String, Map<String, List<FoodIdQuantity>>> getFoodRecordPackage(Map<String, Map<String, FoodItemDetail>> mealData, Date date, String category) {
        Map<String, Map<String, List<FoodIdQuantity>>> result = new HashMap<>();
//         使用 Java 9+ 的 List.of() 方法初始化列表
        List<String> mealTypes = List.of("breakfast", "lunch", "dinner");
        List<String> categories = List.of("staple", "mainDish", "sideDish", "drink", "fruit");

        for (String mealType1 : mealTypes) {
            // 为每个餐食类型创建一个新的内层 Map
            Map<String, List<FoodIdQuantity>> foodItems = new HashMap<>();
            for (String category1 : categories) {
                List<FoodIdQuantity> foodIdQuantities = new ArrayList<>();
                foodItems.put(category1, foodIdQuantities);
            }
            result.put(mealType1, foodItems);
        }

        QueryWrapper<MealRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("meal_time", date)
                .eq("category", category);
        List<MealRecord> mealRecords = mealRecordMapper.selectList(queryWrapper);
        for (MealRecord mealRecord : mealRecords) {
            String mealType = mealRecord.getMealType();
            QueryWrapper<MealRecordDetail> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("meal_record_id", mealRecord.getMealRecordId());
            List<MealRecordDetail> mealRecordDetails = mealRecordDetailMapper.selectList(queryWrapper1);
            Map<Integer, Integer> foodIdQuantityMap = new HashMap<>();
            List<Integer> foodIds = new ArrayList<>();
            for (MealRecordDetail mealRecordDetail : mealRecordDetails) {
                foodIdQuantityMap.put(mealRecordDetail.getFoodId(), mealRecordDetail.getQuantity());
                foodIds.add(mealRecordDetail.getFoodId());
            }
            List<FoodDict> foodDicts = new ArrayList<>();
            if (foodIds.size() > 0) {
                QueryWrapper<FoodDict> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.in("food_id", foodIds);
                foodDicts = foodDictMapper.selectList(queryWrapper2);
            }

            for (FoodDict foodDict : foodDicts) {
                String categoryFood = foodDict.getCategory();
                Integer foodId = foodDict.getFoodId();
                Integer quantity = foodIdQuantityMap.get(foodId);
                FoodIdQuantity foodIdQuantity = new FoodIdQuantity();
                foodIdQuantity.setFoodId(foodId);
                foodIdQuantity.setQuantity(quantity);
                result.get(mealType).get(categoryFood).add(foodIdQuantity);
            }

        }

        for (String mealType : mealTypes) {
            for (String category1 : categories) {
                List<FoodIdQuantity> foodIdQuantities = result.get(mealType).get(category1);
                int size = foodIdQuantities.size();
                for (int i = 0; i < mealData.get(mealType).get(category1).getLimit() - size; i++) {
                    FoodIdQuantity foodIdQuantity = new FoodIdQuantity();
                    foodIdQuantities.add(foodIdQuantity);
                }
            }
        }
        return result;
    }

    @Override
    public void addPackage(Map<String, Map<String, List<FoodIdQuantity>>> customerFoods, Date date, String category) {
        QueryWrapper<MealRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", category)
                .eq("meal_time", date);
        mealRecordMapper.delete(queryWrapper);
        // 遍历外层 Map
        for (Map.Entry<String, Map<String, List<FoodIdQuantity>>> outerEntry : customerFoods.entrySet()) {
            String mealType = outerEntry.getKey();
            Map<String, List<FoodIdQuantity>> mealFoods = outerEntry.getValue();
            MealRecord mealRecord1 = new MealRecord();
            mealRecord1.setMealType(mealType);
            mealRecord1.setMealTime(date);
            mealRecord1.setCategory(category);
            mealRecordMapper.insert(mealRecord1);

            Integer mealRecordId = mealRecord1.getMealRecordId();
            // 遍历内层 Map
            for (Map.Entry<String, List<FoodIdQuantity>> innerEntry : mealFoods.entrySet()) {
                String foodType = innerEntry.getKey();
                List<FoodIdQuantity> foodList = innerEntry.getValue();

                // 遍历 FoodIdQuantity 列表
                for (FoodIdQuantity food : foodList) {
                    Integer foodId = food.getFoodId();
                    Integer quantity = food.getQuantity();
                    MealRecordDetail mealRecordDetail = new MealRecordDetail();
                    mealRecordDetail.setMealRecordId(mealRecordId);
                    mealRecordDetail.setFoodId(foodId);
                    mealRecordDetail.setQuantity(quantity);
                    mealRecordDetailMapper.insert(mealRecordDetail);
                }
            }
        }
    }
}
